<?php
declare (strict_types = 1);

namespace app\admin\model\project;

use bbweb\traits\ModelTrait;
use bbweb\basic\BaseModel;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class Projects extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'projects';

    // 设置json类型字段
    protected $json = ['progress_list'];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    use ModelTrait;


    // 设置字段信息
    protected $schema = [
        'id'			=> 'int',
        'name'		    => 'varchar',
        'description'	=> 'varchar',
        'sys'			=> 'tinyint',
        'type'		    => 'int',
        'uid'		    => 'int',
        'owner_id'	    => 'varchar',
        'teams'		    => 'varchar',
        'audit_id'	    => 'varchar',
        'must_audit'    => 'tinyint',
        'status'		=> 'tinyint',
        'is_delete'     => 'tinyint',
        'progress_list' => 'varchar',
        'images'        => 'text',
        'create_at'     => 'int',
        'finish_at'     => 'int',
        'start_at'      => 'int',
        'end_at'        => 'int',
        'update_at'     => 'int',
    ];

    /**
     * 统计项目各栏目数量
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function toolTask($id)
    {
        $columns = Db::name('project_columns')->where('project_id', $id)->field('title,position')->order('position ASC')->select();
        $list = [];
        foreach ($columns as $column) {
            $list[$column['position']] = [
                'title' => $column['title'],
                'total' => Db::name('project_tasks')->where('project_id', $id)->where('column_id', $column['position'])->where('is_delete',0)->count('id')
            ];
        }
        if( self::update(['progress_list' => $list,'update_at'=>time()], ['id' => $id]) ) return true;
        return false;
    }
}
